﻿/* License
 * This work is licensed under the Creative Commons Attribution 2.5 License
 * http://creativecommons.org/licenses/by/2.5/

 * You are free:
 *     to copy, distribute, display, and perform the work
 *     to make derivative works
 *     to make commercial use of the work
 * 
 * Under the following conditions:
 *     You must attribute the work in the manner specified by the author or licensor:
 *          - If you find this component useful a email to [sobne.cn@gmail.com] would be appreciated.
 *     For any reuse or distribution, you must make clear to others the license terms of this work.
 *     Any of these conditions can be waived if you get permission from the copyright holder.
 * 
 * Copyright sobne.cn All Rights Reserved.
*/
using System;
using System.Data;
using System.Collections.Generic;

namespace Lib4Net
{
    /// <summary>
    /// 基类
    /// </summary>
    public class Base
    {
        /// <summary>
        /// 比较大小,使用范型,返回较大的那个数
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="op1">数据1</param>
        /// <param name="op2"><数据2/param>
        /// <returns>较大的那个数</returns>
        public static T Max<T>(T op1, T op2) where T : IComparable
        {
            if (op1.CompareTo(op2) > 0)
                return op1;
            return op2;
        }
        /// <summary>
        /// 返回两个数中较大的那个
        /// </summary>
        public static Func<int, int, int> Max1 = (a, b) => a > b ? a : b;
        /// <summary>
        /// 返回两个数中较大的那个
        /// </summary>
        public static Func<int, int, int> Max11 = delegate(int a, int b)
        {
            if (a > b)
                return a;
            else
                return b;
        };
        /// <summary>
        /// 交换DataTable中的行列位置
        /// </summary>
        /// <param name="inputDT">要交换的DataTable</param>
        /// <returns>交换后的DataTable</returns>
        private static DataTable SwapDTCR(DataTable inputDT)
        {
            DataTable outputDT = new DataTable();

            //标题的位置不变
            outputDT.Columns.Add(inputDT.Columns[0].ColumnName.ToString());

            foreach (DataRow inRow in inputDT.Rows)
            {
                string newColName = inRow[0].ToString();
                outputDT.Columns.Add(newColName);
            }

            for (int rCount = 1; rCount <= inputDT.Columns.Count - 1; rCount++)
            {
                DataRow newRow = outputDT.NewRow();

                newRow[0] = inputDT.Columns[rCount].ColumnName.ToString();
                for (int cCount = 0; cCount <= inputDT.Rows.Count - 1; cCount++)
                {
                    string colValue = inputDT.Rows[cCount][rCount].ToString();
                    newRow[cCount + 1] = colValue;
                }
                outputDT.Rows.Add(newRow);
            }
            return outputDT;
        }
        /// <summary>
        /// 过滤行(从第0行开始)
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        public static DataTable tableFilter(DataTable dt, int start, int end)
        {
            DataTable dtnew = new DataTable();
            dtnew = dt.Clone();
            for (int i = start; i < end; i++)
            {
                dtnew.Rows.Add(dt.Rows[i].ItemArray);
            }
            return dtnew;
        }
    }
}
